package org.smart4j.chapter4;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class CGLibProxy implements MethodInterceptor {
    private static final CGLibProxy instance = new CGLibProxy();
    private CGLibProxy(){

    }
    @Override
    public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
        System.out.println("代理开始");
        Object result = methodProxy.invokeSuper(o, args);
        System.out.println("代理要结束");
        return result;
    }

    public static CGLibProxy getInstance(){
        return instance;
    }

    public <T> T getProxy(Class<T> cls){
        return (T) Enhancer.create(cls, this);
    }

    public static void main(String[] args) {
        CGLibProxy instance = getInstance();
        Test proxy = instance.getProxy(Test.class);
        proxy.hello();

    }

}
class Test{
    public void hello(){
        System.out.println("hello");
    }
}